package com.xinbochuang.machine.framework.web.service;

import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.common.constant.Constants;
import com.xinbochuang.machine.common.domain.LoginUser;
import com.xinbochuang.machine.common.exception.CustomException;
import com.xinbochuang.machine.common.exception.user.CaptchaException;
import com.xinbochuang.machine.common.exception.user.CaptchaExpireException;
import com.xinbochuang.machine.common.exception.user.UserPasswordNotMatchException;
import com.xinbochuang.machine.common.redis.RedisCache;
import com.xinbochuang.machine.framework.security.custom.IdCardAuthenticationToken;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 登录校验方法
 *
 * @author 黄晓鹏
 * @date 2020-09-09 13:48
 */
@Component
public class SysLoginService {

    @Resource
    private TokenService tokenService;

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private RedisCache redisCache;

    @Resource
    private UserDetailsServiceImpl userDetailsService;

    /**
     * 登录验证
     *
     * @param username 用户名
     * @param password 密码
     * @param code     验证码
     * @param uuid     唯一标识
     * @return 结果
     */
    public String login(String username, String password, String code, String uuid) {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        // 验证码失效
        if (captcha == null) {
            throw new CaptchaExpireException();
        }
        // 验证码错误
        if (!code.equalsIgnoreCase(captcha)) {
            throw new CaptchaException();
        }
        // 用户验证
        Authentication authentication = null;
        try {
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
        } catch (Exception e) {
            if (e instanceof BadCredentialsException) {
                throw new UserPasswordNotMatchException();
            } else {
                throw new CustomException(e.getMessage());
            }
        }
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        // 生成token
        return tokenService.createToken(loginUser);
    }

    /**
     * app通过身份证号登陆验证
     *
     * @param idCard 身份证号
     */
    public void appLogin(String idCard) {
        // 用户验证
        Authentication authentication = null;
        try {
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            authentication=authenticationManager.authenticate(new IdCardAuthenticationToken(idCard));
        } catch (Exception e) {
            if (e instanceof BadCredentialsException) {
                throw new UserPasswordNotMatchException();
            } else {
                throw new CustomException(e.getMessage());
            }
        }
    }
}
